package org.example.yuaiagent.rag;

import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.document.Document;
import org.springframework.ai.reader.markdown.MarkdownDocumentReader;
import org.springframework.ai.reader.markdown.config.MarkdownDocumentReaderConfig;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@Slf4j
@Component
public class TravelAppDocumentLoader {
    private final ResourcePatternResolver resourcePatternResolver;
     public TravelAppDocumentLoader(ResourcePatternResolver resourcePatternResolver){
         this.resourcePatternResolver = resourcePatternResolver;
     }
      public List<Document> loadMarkdowns(){
          List<Document> allDocuments = new ArrayList<>();
          //加载多个markdown文件
           try {
                Resource[] resources = resourcePatternResolver.getResources("classpath:document/*.md");
                for (Resource resource : resources) {
                    String fileName = resource.getFilename();
                    MarkdownDocumentReaderConfig config = MarkdownDocumentReaderConfig.builder()
                            .withHorizontalRuleCreateDocument(true) // 将水平线标记为独立文档
                            .withIncludeCodeBlock(false)            // 排除代码块内容
                            .withIncludeBlockquote(false)          // 排除引用块内容
                            .withAdditionalMetadata("filename", fileName) // 添加文件名作为元数据
                            .build();
                    MarkdownDocumentReader reader = new MarkdownDocumentReader(resource, config);
                    allDocuments.addAll(reader.get());
                }
          } catch (IOException e) {
              log.error("Markdown 文档加载失败", e);
          };
           return allDocuments;
      }

}
